package com.apache.bid.service.plugins;

import org.apache.log4j.Logger;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.bid.entity.BidPrintParam;
import com.apache.bid.manager.BidPrintParamManager;
import com.apache.cache.util.Validator;

/**
 * description:  根据sessionId/itemId/printId获取BidPrintParam
 * <p>参数:objId、objType
 * <p>objType：sessionId、itemId、printId
 * @author ljshi 创建时间：2016-5-17  
 * 
 */
public class BidPrintParamEntityPlugin implements PluginConnector {

	private Logger logger = Logger.getLogger(BidPrintParamEntityPlugin.class);
	private BidPrintParamManager bidPrintParamManager;

	@Override
	public Object execute(ParamsVo vo) throws Exception {

		ResultEntity result = new ResultEntity();
		result.setResult("true");
		String objId = String.valueOf(vo.getParams("objId"));
		String objType = String.valueOf(vo.getParams("objType"));
		if (Validator.isNull(objId) || Validator.isNull(objType)) {
			result.setResult("false");
			result.setMessage("缺少必要的参数！");
			return result;
		}

		ParamsVo<BidPrintParam> paraVo = new ParamsVo<BidPrintParam>();
		BidPrintParam para = new BidPrintParam();
		try {
			if ("sessionId".equals(objType)) {
				para.setSessionId(objId);
			} else if ("itemId".equals(objType)) {
				para.setItemId(objId);
			} else if ("printId".equals(objType)) {
				para.setPrintId(objId);
			} else {
				result.setResult("false");
				result.setMessage("参数错误！");
				return result;
			}

			paraVo.setObj(para);
			paraVo.setMethodKey("singleParaByAttr");
			BidPrintParam bidParam = (BidPrintParam) bidPrintParamManager.execute(paraVo);
			if (bidParam == null) {
				result.setResult("false");
				result.setMessage("查询失败！");
			} else {
				result.setMessage("查询成功！");
				result.setEntity(bidParam);
			}
		} catch (Exception e) {
			result.setResult("false");
			result.setMessage("查询错误！");
			logger.error("根据" + objType + ":" + objId + "查询单个para对象错误！" + e.getMessage());
		}
		return result;
	}

	public void setBidPrintParamManager(BidPrintParamManager bidPrintParamManager) {
		this.bidPrintParamManager = bidPrintParamManager;
	}

}
